﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Foasis.Video;

namespace AVSRadian
{
	public partial class ViewBox : UserControl
	{
		public ViewBox()
		{
			InitializeComponent();
			this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
			this.Start = false;
			this.mouseStartPosition = Point.Empty;
			this.mouseEndPosition = Point.Empty;
			this.mouseLeft = false;
			this.selectMode = false;
		}
#if XNA
#else
		private Bitmap frame;
#endif
#if DEBUG
		private int timeindex = 0;
#endif
		/// <summary>
		/// 鼠标起始控件位置。
		/// </summary>
		private Point mouseStartPosition;
		/// <summary>
		/// 鼠标左键是否按下。
		/// </summary>
		private bool mouseLeft;
		/// <summary>
		/// 鼠标目前控件位置。
		/// </summary>
		private Point mousePosition;
		/// <summary>
		/// 鼠标结束控件位置。
		/// </summary>
		private Point mouseEndPosition;
		/// <summary>
		/// 是否为选择状态。
		/// </summary>
		private bool selectMode;

		public DateTime Test_start
		{
			get;
			set;
		}

		public DateTime Test_end
		{
			get;
			set;
		}

		public bool Start
		{
			get;
			set;
		}

		public byte[] FrameByte
		{
			get;
			set;
		}
		public Bitmap Frame
		{
			get
			{
				return this.frame;
			}
		}

		/// <summary>
		/// 视频接口。
		/// </summary>
		public IVideo VideoAttribute
		{
			get;
			set;
		}

		//private Brush br = Brushes.Black;
		////Graphics g;// = Graphics.FromImage();

		protected override void OnPaintBackground(PaintEventArgs e)
		{
			//base.OnPaintBackground(e);
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			//base.OnPaint(e);
			if (this.Start)
			{
#if DEBUG
				this.Test_start = DateTime.Now;
#endif
#if XNA
				this.Frame = this.FrameInfo.GetFrame();
#else
				this.VideoAttribute.GetFrame(ref this.frame);
#endif
			}

#if XNA
			SpriteBatch sb = new SpriteBatch(this.gd);
			this.gd.Clear(XnaColor.LightGray);
			if (!this.start)
			{
				this.gd.Present();
			}
			else
			{
				Bitmap[] bpg = { this.bp };
				XnaColor[] xxx = { XnaColor.Tomato, XnaColor.Yellow, XnaColor.Blue, XnaColor.Green };
				byte[] test = { 255, 0, 0, 255 };
				sb.Begin();
				Texture2D t2d = new Texture2D(this.gd, 720, 480);
				t2d.SetData<byte>(this.Frame);
				sb.Draw(t2d, this.gd.ScissorRectangle, null, XnaColor.White, 0, new Vector2(), SpriteEffects.None, 0);
				sb.End();
				this.gd.Present();
				this.test_end = DateTime.Now;
			}
#else
			if (this.Frame == null)
				e.Graphics.Clear(System.Drawing.Color.LightGray);
			else
			{
				Pen myPen = new Pen(Color.FromArgb(255, 51, 153, 255), 1);
				myPen.DashCap = System.Drawing.Drawing2D.DashCap.Flat;
				myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;

				Brush myBrush = new SolidBrush(Color.FromArgb(120, 51, 153, 255));
				

				if (this.selectMode)
				{
					using (Graphics myGraphics = Graphics.FromImage(this.frame))
					{
						Rectangle r = new Rectangle();
						r.Location = this.mouseStartPosition;

						if (this.mouseLeft)
							r.Size = new Size(this.mousePosition.X - this.mouseStartPosition.X, this.mousePosition.Y - this.mouseStartPosition.Y);
						else
							r.Size = new Size(this.mouseEndPosition.X - this.mouseStartPosition.X, this.mouseEndPosition.Y - this.mouseStartPosition.Y);

						myGraphics.DrawRectangle(myPen, r);

						r.Location = new Point(this.mouseStartPosition.X + 1, this.mouseStartPosition.Y + 1);

						if (this.mouseLeft)
							r.Size = new Size(this.mousePosition.X - this.mouseStartPosition.X - 1, this.mousePosition.Y - this.mouseStartPosition.Y - 1);
						else
							r.Size = new Size(this.mouseEndPosition.X - this.mouseStartPosition.X - 1, this.mouseEndPosition.Y - this.mouseStartPosition.Y - 1);
						myGraphics.FillRectangle(myBrush, r);

						
					}
				}
				//Graphics myGraphics = Graphics.FromImage(this.frame);
				//myGraphics.DrawRectangle(myPen, 10, 10, 100, 100);
				e.Graphics.DrawImage(this.frame, Point.Empty);
				
				//myGraphics.Dispose();
				myBrush.Dispose();
				myPen.Dispose();

#if DEBUG
				this.Test_end = DateTime.Now;
#endif
			}
#endif


			if ((!this.DesignMode) && this.Start)
			{
				base.Invalidate();
				//this.Start = false;
			}
		}

		protected override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown(e);
			if (e.Button == MouseButtons.Left)
			{
				this.mouseLeft = true;
				this.selectMode = true;
				this.mouseStartPosition = e.Location;
			}
		}

		protected override void OnMouseUp(MouseEventArgs e)
		{
			base.OnMouseUp(e);
			if (e.Button == MouseButtons.Left)
			{
				this.mouseLeft = false;
				this.mouseEndPosition = e.Location;
			}
			else if (e.Button == MouseButtons.Right)
			{
				this.selectMode = false;
			}
			int y = new int();
		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			this.mousePosition = e.Location;
		}
	}
}
